//
//  TJOriginalView.swift
//  WeiBo
//
//  Created by TanJian on 15/12/10.
//  Copyright © 2015年 Joshpell. All rights reserved.
//

import UIKit
import SnapKit

let TJMargin:CGFloat = 10
let TJScreen:CGSize = UIScreen.mainScreen().bounds.size

//原创微博内容ciew
class TJOriginalView: UIView {
    
    //视图的底部约束
    var bottomConstraint: Constraint?
    //获取到视图模型时数据赋值
    var statusViewModel:TJUserDataViewModel?{
        didSet{
            iconView.sd_setImageWithURL(NSURL(string: (statusViewModel?.status?.user?.profile_image_url)!),placeholderImage: UIImage(named: "avatar_default_big"))
            nameLable.text = statusViewModel?.status?.user?.name
            timeLable.text = "下一秒"
            sourceLable.text = "黑马11"
            vipIconView.image = statusViewModel?.headIcon

            verifiedImageView.image = statusViewModel?.verifiedImage
            
            contentLable.text = statusViewModel?.status?.text
            
            //原创微博配图数据
            bottomConstraint?.uninstall()
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0{
                picView.pic_urls = pic_urls
                picView.hidden = false
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(picView).offset(TJCellMargin).constraint
                })
            }else{
                picView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLable).offset(TJCellMargin).constraint
            })
                
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI() {
        
        addSubview(iconView)
        addSubview(nameLable)
        addSubview(vipIconView)
        addSubview(timeLable)
        addSubview(sourceLable)
        addSubview(verifiedImageView)
        addSubview(contentLable)
        addSubview(picView)
        
        //头像约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(TJMargin)
            make.leading.equalTo(self).offset(TJMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        //昵称
        nameLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(TJMargin)
        }
        //会员图标
        vipIconView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLable)
            make.leading.equalTo(nameLable.snp_trailing).offset(TJMargin)
        }
        //时间标志
        timeLable.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLable)
        }
        //来源
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLable)
            make.leading.equalTo(timeLable.snp_trailing).offset(TJMargin)
        }
        //认证
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(iconView.snp_bottom)
            make.centerX.equalTo(iconView.snp_trailing)
        }
        //原创微博的内容约束
        contentLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(TJMargin)
        }
        //配图的约束
        picView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLable)
            make.top.equalTo(contentLable.snp_bottom).offset(TJCellMargin)
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //最重要的约束,自身view的高度根据内容变化
        snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(picView).offset(TJCellMargin).constraint
        }
        
    }
    
    //头像
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //昵称
    private lazy var nameLable:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
    //时间
    private lazy var timeLable:UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 10)
    //来源
    private lazy var sourceLable:UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 10)
    //会员图标
    private lazy var vipIconView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //微博认证
    private lazy var verifiedImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //微博内容
    private lazy var contentLable:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15, maxWidth: TJScreen.width - 2 * TJMargin)
    
    //原创微博的配图
    private lazy var picView:TJStatusPicView = {
        let view = TJStatusPicView()
        view.backgroundColor = self.backgroundColor
        
        return view
    }()
    
}








